package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.entity.Result;
import com.itheima.mapper.UserMapper;
import com.itheima.pojo.User;
import com.itheima.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public Result login(String username, String password) {
        //1、判断传入数据是否为空
        if (username==null||password==null){
            return new Result(false,"数据异常" );
        }
        //2、根据传入的username查询对象数据
        LambdaQueryWrapper<User> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(User::getUsername,username);
        User userDb = userMapper.selectOne(wrapper);
        //3、判断数据库中是否有该对象
        if (userDb==null){
            return new Result(false,"该用户不存在" );
        }
        //4.1、把传入的密码加密
        String checkPwd = DigestUtils.md5DigestAsHex(password.getBytes());
        //4.2、匹配数据库信息 查询密码是否正确
        if (!userDb.getPassword().equals(checkPwd)){
            return new Result(false, "密码不正确");
        }
        return new Result(true,"登陆成功",userDb);
    }
}
